package engine;

import java.rmi.AccessException;
import java.rmi.Remote;
import java.rmi.RemoteException;

import compute.Compute;
import compute.Task;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import static java.lang.System.out;

public class ComputeEngine implements Compute, Remote {

	public ComputeEngine() {
		super();
	}
	
	public <T> T executeTask(Task<T> t){
		return t.execute();
	}

	public static void main(String[] args){
		System.out.println(
				System.getProperty("java.rmi.server.codebase"));
		String strClassCodebase = Compute.class.getProtectionDomain().getCodeSource().getLocation().toString();
		System.setProperty("java.rmi.server.codebase", strClassCodebase);
		p(strClassCodebase);
		p(Compute.class.getName());
		if (System.getSecurityManager() != null) {
			System.out.println("Security manager installed!! Error!"); System.exit(1);
		}

			try {
				String name = "Compute";
				Compute engine = new ComputeEngine();
				Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
				LocateRegistry.createRegistry(1099);
				Registry registry = LocateRegistry.getRegistry();
				registry.rebind(name, stub);
				System.out.println("ComputeEngine bound");
				for (String s: registry.list()){
					System.out.println(s);
				}
			} catch (AccessException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			
	}
		public static void p(Object objects){
			out.println(objects.toString());
		}
}
